package com.baizhi.controller;

import com.baizhi.entity.Address;
import com.baizhi.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @ResponseBody//该方法返回是一个具体值,不是一个jsp
    @RequestMapping("/check")
    public String checkUserName(String username){
        if(username.equals("zhangsan")){
            return "false";
        }
        return "true";
    }
    @ResponseBody//该方法返回是一个具体值,不是一个jsp
    @RequestMapping("/check2")
    public String checkUserName2() throws JsonProcessingException {
        User user = new User(1,"zhangsan",20,new Address(1,"zhengzhou","文化路"));
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }

    @ResponseBody//该方法返回是一个具体值,不是一个jsp
    @RequestMapping("/selectAll")
    public List<User> selectAll() throws JsonProcessingException {
        User user1 = new User(1,"zhangsan",20,new Address(1,"zhengzhou","文化路"));
        User user2 = new User(2,"lisi",20,new Address(1,"zhengzhou","文化路"));
        List<User> userList=new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        return userList;
    }

    @ResponseBody//该方法返回是一个具体值,不是一个jsp
    @RequestMapping("/check3")
    public User checkUserName3() throws JsonProcessingException {
        User user = new User(1,"zhangsan",20,new Address(1,"zhengzhou","文化路"));
        return user;
    }

}
